﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using XRM.Security.WebUI.Models.InfoSystem;

namespace XRM.Security.WebUI.Models.UserRegister
{
    public class UserRegisterInfoSystemEditModel : BaseViewModel, IValidatableObject
    {
        public UserRegisterInfoSystemEditModel()
        {
            InfoSystemList = new List<UserInfoSystemModel>();
        }

        public void GetUserRegister( DataAccess.Models.UserRegister dbEntity )
        {
            UserRegister = new UserRegisterModel( dbEntity );
        }

        public int UserRegisterId { get; set; }
        public UserRegisterModel UserRegister { get; set; }

        [Display( Name = "Информационная система" )]
        public int InfoSystemId { get; set; }
        [Display( Name = "Отображать системы, к которым доступ был закрыт" )]
        public bool ShowRevoke { get; set; }

        public List<UserInfoSystemModel> InfoSystemList { get; set; }
        public List<InfoSystemModel> InfoSystemAllList { get; set; }

        public void GetInfoSystemAll( List<DataAccess.Models.InfoSystem> listAll )
        {
            InfoSystemAllList = new List<InfoSystemModel>();

            if ( listAll != null && listAll.Any() )
            {
                listAll = listAll.OrderBy( x => x.InfoSystemName ).ToList();
                foreach ( var item in listAll )
                {
                    InfoSystemAllList.Add( new InfoSystemModel( item ) );
                }
                if ( InfoSystemId <= 0 )
                    InfoSystemId = InfoSystemAllList.First().Id.Value;
            }
        }

        public IEnumerable<ValidationResult> Validate( ValidationContext validationContext )
        {
            yield break;
        }
    }
}